package com.cheetah.start.system.dict.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.cheetah.start.common.annotation.ApiCount;
import com.cheetah.start.common.annotation.Log;
import com.cheetah.start.common.base.result.Result;
import com.cheetah.start.common.base.result.ResultController;
import com.cheetah.start.common.enums.BusinessTypeEnum;
import com.cheetah.start.common.enums.LogTypeEnum;
import com.cheetah.start.system.dict.domain.SysDictData;
import com.cheetah.start.system.dict.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author Feel
 * @create 2021/7/18
 **/
@Api(tags = "字典数据")
@Slf4j
@ApiCount
@Validated
@CrossOrigin
@RestController
@RequestMapping("/dictData")
public class SysDictDataController extends ResultController {

    @Autowired
    private SysDictDataService dictDataService;



    /**
     * 编辑页面
     *
     * @param dictDataId dict数据id
     * @return {@link ModelAndView}
     */
    @ApiOperation(value = "编辑字典数据页面")
    @GetMapping("/edit")
    public ModelAndView edit(String dictDataId) {
        SysDictData dictData = dictDataService.getById(dictDataId);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("sysDictData", dictData);
        modelAndView.setViewName("/dict/data/edit");
        return modelAndView;
    }



    /**
     * 启用
     *
     * @param sysDictData sys dict 类型数据
     * @return {@link Result}
     */
    @SaCheckPermission("sys:dictData:switch")
    @ApiOperation(value = "启用字典数据")
    @PostMapping("/enable")
    @Log(title = "字典数据", describe = "启用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result enable(@RequestBody SysDictData sysDictData) {
        SysDictData byId = dictDataService.getById(sysDictData.getDictDataId());
        byId.setEnable("0");
        return decide(dictDataService.updateById(byId), BusinessTypeEnum.EDIT);
    }


    /**
     * 禁用
     *
     * @param sysDictData sys dict类型数据
     * @return {@link Result}
     */
    @ApiOperation(value = "禁用字典数据")
    @PostMapping("/disable")
    @Log(title = "字典数据", describe = "禁用", type = BusinessTypeEnum.EDIT, logType = LogTypeEnum.OPERATE)
    public Result disable(@RequestBody SysDictData sysDictData) {
        SysDictData byId = dictDataService.getById(sysDictData.getDictDataId());
        byId.setEnable("1");
        return decide(dictDataService.updateById(byId), BusinessTypeEnum.EDIT);
    }

    /**
     * 通过代码
     *
     * @param code 代码
     * @return {@link Result}
     */
    @ApiOperation(value = "获取字典")
    @GetMapping("/getByCode")
    public Result getByCode(String code){
        List<SysDictData> byDictCode = dictDataService.getByDictCode(code);
        return success(byDictCode);
    }


}
